#ifndef _XOBJ_H_
#define _XOBJ_H_

#include "..\cuCommon\cuTypes.h"
#include "..\rtCommon\RegularGrid.h"
#include "..\rtCommon\Plane.h"
#include "..\rtCommon\Ray.h"
#include "..\rtCommon\Triangle.h"
#include "..\rtCommon\Box.h"

#include <vector>

#define XOBJ_OK                   0             // objects were loaded
#define XOBJ_FILE_NOT_FOUND       1             // file path is incorrect
#define XOBJ_INVALID_ARGS         2             // invalid parameters 
#define XOBJ_INVALID_FILE_FORMAT  3             // invalid file format (typically some expected values are missing) 

#define XOBJ_MAX_STRING_LENGTH   1024
#define XOBJ_N_TEXMAPS           12

struct SMaterial
{
  float4 ambient;	
  float4 diffuse;
  float4 specular; 
  float4 emission;

	float  shininess;
};

SMaterial * Material(SMaterial * pMaterial);

class CXobjIO
{
public:

	S_Box m_AABB;               // axis aligned bounding box

  // TODO : add oriented bounding box and bounding sphere

	matrix4x4 matrixWS;             // transform to world space

	SMaterial material;
	
	std::vector<float4> pos;            // vertices - float4 because of CUDA restrictions on reading float3s
	std::vector<float4> nml;            // per vertex smooth normals
  std::vector<float4> tnt;            // per vertex smooth tangent
  std::vector<float4> bnt;            // per vertex smooth bitangent
  std::vector<float4> m_FaceNormal;   // per face normal
	std::vector<float4> clr;            // per vertex colors
  std::vector<float2> uv;             // uv coordinates for regular 2D texture mapping                                                          

	std::vector<int>  posIdx;           // indexes vertex position, vertex normals in .xyz and face normal in .w
	std::vector<int4> uvIdx;            // indexes vertex uv coordinates in .xyz
	std::vector<int4> clrIdx;           // indexes vertex colors in .xyz

	bool m_bIsLight;

	CXobjIO(void);
	CXobjIO(const CXobjIO & xobj);
	~CXobjIO(void);

  static int Load(std::vector<CXobjIO *> &vXobjList, const char * fname);

  void TransformWS(void);

	void Release(void);

	int  FaceCount() { return posIdx.size() / 3; }
};

void FaceNormal ( CXobjIO * pXobjIO );
void TangentBasis    ( CXobjIO * pXobjIO );

#endif // _XOBJ_H_